package com.tarena.practice.manage.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tarena.practice.manage.mapper.DetailMapper;
import com.tarena.practice.manage.mapper.UserMapper;
import com.tarena.practice.manage.pojo.Detail;
import com.tarena.practice.manage.pojo.User;
import com.tarena.practice.manage.util.PageUtil;
import com.tarena.practice.manage.vo.Page;
import com.tarena.practice.manage.vo.SysResult;
import com.tarena.practice.manage.vo.UserResult;

@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private DetailMapper detailMapper;
	@Autowired
	private PageUtil pageUtil;

	public SysResult queryUsersByPage(Page page) {
		SysResult result = new SysResult();
		// 获取总记录数
		int totalCount = userMapper.getCount(page);
		page.setTotalCount(totalCount);
		// 计算总页数
		int totalPage = (totalCount % pageUtil.getPageSize() == 0) ? (totalCount / pageUtil.getPageSize())
				: (totalCount / pageUtil.getPageSize()) + 1;
		page.setTotalPage(totalPage);
		page.setPageSize(pageUtil.getPageSize());
		// 计算前一页
		if (page.getCurrentPage() == 1) {
			page.setPreviousPage(1);
		} else {
			page.setPreviousPage(page.getCurrentPage() - 1);
		}
		// 计算后一页
		if (page.getCurrentPage() == totalPage) {
			page.setNextPage(totalPage);
		} else {
			page.setNextPage(page.getCurrentPage() + 1);
		}
		// 获取当前页数据
		List<UserResult> users = userMapper.getUsersByPage(page);
		page.setData(users);
		// 获取分页组件中超链接的个数
		page.setaNum(pageUtil.getFenYe_a_Num(page.getCurrentPage(), pageUtil.getPageSize(), totalCount, totalPage));
		// 执行到此的时候说明page里的数据全部填充完毕,准备封装result对象
		result.setStatus(200);
		result.setMessage("数据查询到了");
		result.setData(page);
		return result;
	}

	public SysResult deleteUsersById(int[] ids) {
		SysResult result = new SysResult();
		Detail detail = new Detail();
		for(int i = 1; i < ids.length; i++){
			detail.setUserId(ids[i]);
			userMapper.deleteByPrimaryKey(ids[i]);
			detailMapper.delete(detail);
		}
		result.setStatus(200);
		return result;
	}

	public SysResult updateUser(UserResult userResult) {
		User _user = new User();
		_user.setUserId(userResult.getUserId());
		_user.setUserName(userResult.getUserName());
		_user.setPhoneNum(userResult.getPhoneNum());
		_user.setEmail(userResult.getEmail());
		userMapper.updateByPrimaryKeySelective(_user);
		if(StringUtils.isNotEmpty(userResult.getPersonSign())){
			Detail detail = new Detail();
			int userId= userResult.getUserId();
			detail.setUserId(userId);
			detail.setPersonSign(userResult.getPersonSign());
			Integer detailId = detailMapper.selectDetailIdByUserId(userId);
			if(detailId == null){
				detail.setCreateTime(new Date());
				detail.setUpdateTime(detail.getCreateTime());
				detailMapper.insert(detail);
			}else{
				detail.setDetailId(detailId);
				detail.setUpdateTime(new Date());
				detailMapper.updateByPrimaryKeySelective(detail);
			}
		}
		return SysResult.oK();
	}

}
